package com.shr.xsunmvc.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * 全局配置
 * @author sun haoran
 *
 */
public class ConfigLoader {
	
	/**
	 * 存储配置信息的map对象
	 */
	private Map<String, Object> configMap ;
	
	public ConfigLoader(){
		configMap = new HashMap<String, Object>();
	}
	
	
	public void load(String confPath){
		Properties properties = new Properties() ;
		try {
			properties.load(new FileInputStream(confPath));
			toMap(properties);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	private void toMap(Properties properties) {
		if(properties != null){
			Set<Object> keys = properties.keySet() ;
			for(Object key : keys){
				Object value  = properties.get(key);
				configMap.put(key.toString(), value) ;
			}
		}
	}
	
	/**
	 * 根据key值获取配置对象对应的字符串
	 * @param key
	 * @return
	 */
	public String getConf(String key){
		Object value = configMap.get(key) ;
		if(value != null)
			return value.toString() ;
		return null ;
	}
	
	/**
	 * 设置配置对象
	 * @param key
	 * @param value
	 */
	public void setConf(String key, Object value){
		configMap.put(key, value) ;
	}
	/**
	 * 根据key获取对象
	 * @param key
	 * @return value Object
	 */
	public Object getConfObject(String key){
		return configMap.get(key) ;
	}
}
